<?php
declare (strict_types = 1);

namespace app\controller\admin;

use app\controller\admin\AdminBaseController;
use think\Request;

use app\model\admin\SysDictData as SysDictDataModel;

class SysDictData extends AdminBaseController
{

    public function index(Request $request)
    {
        $field = "*";
        $data = SysDictDataModel::scope('where', $request->param())
            ->scope('dataFilter')
            ->field($field)
            ->order('id','desc')
            ->paginate([
                'list_rows' => $this->limit,
                'page' => $this->page,
                'var_page' => $this->pageVar
            ]);
        return successPage($data);
    }

    public function save(Request $request)
    {
        $param = $request->param();
        $css_class = $param['css_class'] ?? '';
        $dict_label = $param['dict_label'] ?? '';
        $dict_sort = $param['dict_sort'] ?? 0;
        $dict_type = $param['dict_type']  ?? '';
        $dict_value = $param['dict_value']  ?? '';
        $list_class = $param['list_class']  ?? '';
        $remark = $param['remark']  ?? '';
        $status = $param['status']  ?? '0';

        $data = SysDictDataModel::create([
            'css_class' => $css_class,
            'dict_label' => $dict_label,
            'dict_sort' => $dict_sort,
            'dict_type' => $dict_type,
            'dict_value' => $dict_value,
            'list_class' => $list_class,
            'remark' => $remark,
            'status' => $status,
        ]);
        if($data->id){
            return success($data);
        }
        return error('添加失败');
    }



    public function show(Request $request)
    {
        $id = $request->param('id');
        $data = SysDictDataModel::where('id', $id)->scope('dataFilter')->find();
        if(!$data){
            return error('数据不存在');
        }
        return success($data);
    }

    public function update(Request $request)
    {
        $id = $request->param('id');
        if(!$id){
            return error('缺少必要参数');
        }
        
        $model = SysDictDataModel::where('id', $id)->scope('dataFilter')->find();

        $data = $request->only(['css_class','dict_label','dict_sort','dict_value','list_class','remark','status']);
        $ret = $model->save($data);
        if(!$ret){
            return error('更新失败');
        }
        return success();
    }
    public function delete(Request $request)
    {
        $id = $request->param('id', '');
        if(!$id){
            return error('缺少必要参数');
        }
        $ids = explode(',', $id);
        $ret = SysDictDataModel::destroy($ids);
        if(!$ret){
            return error('删除失败');
        }
        return success();
    }


}
